package com.example.tellwin.home.adpater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.R;
import com.example.tellwin.home.bean.ActivitiesBean;
import com.example.tellwin.utils.CommonUtils;
import com.example.tellwin.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : LinZePeng
 * @date : 2020/11/9
 * @Describe ：
 **/
public class HuoDongListAdapter extends RecyclerView.Adapter<HuoDongListAdapter.HuoDongListViewHolder> {

    List<ActivitiesBean>beans = new ArrayList<>();
    OnItemClickListener onItemClickListener;
    Context context;

    public void setBeans(List<ActivitiesBean> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public HuoDongListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new HuoDongListViewHolder(LayoutInflater.from(context).inflate(R.layout.item_huodong, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull HuoDongListViewHolder holder, int position) {
        ActivitiesBean bean = beans.get(position);
        if (position % 4 == 0) {
            holder.lineIv.setImageResource(R.color.act_line_color_1);
            holder.itemActivitiesView.setBackgroundResource(R.drawable.pick_concers_shape);
        } else if (position % 4 == 1) {
            holder.lineIv.setImageResource(R.color.act_line_color_2);
            holder.itemActivitiesView.setBackgroundResource(R.drawable.light_green_concers_shape);
        } else if (position % 4 == 2) {
            holder.lineIv.setImageResource(R.color.act_line_color_3);
            holder.itemActivitiesView.setBackgroundResource(R.drawable.light_yellow_corners_shape);
        } else if (position % 4 == 3) {
            holder.lineIv.setImageResource(R.color.act_line_color_4);
            holder.itemActivitiesView.setBackgroundResource(R.drawable.light_blue_corners_shape);
        }
        if(bean.getActivityStatus() == 1) {
            holder.activitiesTypeTv.setBackgroundResource(R.mipmap.act_type_icon_1);
            holder.activitiesTypeTv.setText("报名中");
        }else if(bean.getActivityStatus() ==2){
            holder.activitiesTypeTv.setBackgroundResource(R.mipmap.act_type_icon_2);
            holder.activitiesTypeTv.setText("投票中");
        }else {
            holder.activitiesTypeTv.setBackgroundResource(R.mipmap.act_type_icon_3);
            holder.activitiesTypeTv.setText("结束");
        }
        holder.activityTitleTv.setText(bean.getActivityName());
        holder.participantTv.setText(context.getResources().getString(R.string.participant , CommonUtils.getParticipateStr(bean.getParticipateType())));
        holder.registrationPopulationTv.setText(context.getResources().getString(R.string.registration_population , bean.getParticipateCount()+""));
        holder.registrationTimeTv.setText(context.getResources().getString(R.string.registration_time , bean.getParticipateTime()));
        GlideUtils.LoadImage(context , bean.getActivityPic() ,holder.activityIv);
        holder.itemActivitiesView.setOnClickListener(v -> {
            if(onItemClickListener!=null){
                onItemClickListener.onItemClick(position, bean);
            }
        });

    }

    @Override
    public int getItemCount() {
        return beans.size();
    }


    class HuoDongListViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout itemActivitiesView;
        ImageView lineIv;
        ImageView activityIv;
        TextView activitiesTypeTv;
        TextView activityTitleTv;
        TextView participantTv;
        TextView registrationPopulationTv;
        TextView registrationTimeTv;
        ImageView shareIv;

        public HuoDongListViewHolder(@NonNull View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View itemView) {
            itemActivitiesView = (RelativeLayout) itemView.findViewById(R.id.item_activities_view);
            lineIv = (ImageView) itemView.findViewById(R.id.line_iv);
            activityIv = (ImageView) itemView.findViewById(R.id.activity_iv);
            activitiesTypeTv = (TextView) itemView.findViewById(R.id.activities_type_tv);
            activityTitleTv = (TextView) itemView.findViewById(R.id.activity_title_tv);
            participantTv = (TextView) itemView.findViewById(R.id.participant_tv);
            registrationPopulationTv = (TextView) itemView.findViewById(R.id.registration_population_tv);
            registrationTimeTv = (TextView) itemView.findViewById(R.id.registration_time_tv);
            shareIv = (ImageView) itemView.findViewById(R.id.share_iv);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(int position ,ActivitiesBean activitiesBean);
    }
}
